package com.imay.live.manager;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.imay.live.activity.family.FamilyParams;
import com.imay.live.activity.family.manager.FamilyManager;
import com.imay.live.activity.family.manager.FamilyUserChangeManager;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.helper.EventHelper;
import com.imay.live.model.NewMailModel;
import com.imay.live.model.SystemNoticeModel;
import com.imay.live.model.UnreadModel;
import com.imay.live.proto.MessageId;
import com.imay.live.proto.ProtoResult;
import com.imay.live.proto.family.FamilyUserManagementNotify;
import com.imay.tbas_api.IMYTBASObject;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;

import static com.imay.live.manager.UnreadManager.TYPE_UNREAD_CALL_LIVE_PROFILE;
import static com.imay.live.manager.UnreadManager.TYPE_UNREAD_MESSAGE;
import static com.imay.live.manager.UnreadManager.TYPE_UNREAD_SYSTEM;
import static com.imay.live.manager.UnreadManager.TYPE_UNREAD_SYSTEM_PROFILE;

/**
 * Created by  nick on 2017/5/6 0006.
 * 私信
 */

public class MessageManager {


    private IMYTBASObject mSocketNotify;
    private IMYTBASObject mNoLoginNotify;

    private static class MessageManagerHolder {
        private static final MessageManager INSTANCE = new MessageManager();
    }

    public static final MessageManager getInstance() {
        return MessageManagerHolder.INSTANCE;
    }

    private MessageManager() {

    }

    /**
     * 用户失效的通知
     */
    public void registerLoseNotify() {
        mNoLoginNotify = new IMYTBASObject(false);
        mNoLoginNotify.stringCommand = EventHelper.EVENT_NO_LOGIN;
        mNoLoginNotify.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject imytbasObject) {
                AccountManager.getInstance().loginLoseEfficacy();
            }
        });
        mNoLoginNotify.addEventListenWithTBASObject();
    }

    public static final class NewWarnType {
//        public int EventType_Follow         = 1; //关注
//        public int EventType_Like           = 2; //赞
//        public int EventType_SendUser       = 3; //@通知事件
//        public int EventType_ReceiveGift    = 4; //收礼
//        public int EventType_SendGift       = 5; //送礼
//        public int EventType_FeedComment    = 6; //评论
//        public int EventType_FeedForwarding = 7; //转发
//        public int EventType_publishFeed    = 8; // 发布


        public static final int IMBubbleCommentType = 6;        //评论
        public static final int IMBubbleMindType = 13;           //被@到
        public static final int IMBubbleGiftType = 7;           //收礼
        public static final int IMBubbleRePostType = 12;         //被转发
        public static final int IMBubblePraiseType = 4;         //被赞
        public static final int IMBubbleFollowType = 2;          //被关注
        public static final int IMBubbleLiveType = 10;          // 直播间
        public static final int IMBubbleCALLType = 11;          // 召唤直播
    }

    /**
     * 添加socket 监听
     */
    public void registerSocketListener() {
        //监听socket
        mSocketNotify = new IMYTBASObject(false);
        mSocketNotify.stringCommand = "SOCK_NOTIFY";
        mSocketNotify.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject object) {
//                Log.e("MessageManager", "onCommandTaskSuccess: " + object.stringData);
                switch (object.socket_msg_id) {
                    case MessageId.snsPersonalMailSendNotify:
                        EventHelper.postMessageNotify(object.socket_content);
                        UnreadModel model = new UnreadModel();
                        model.setValue(1);
                        model.setWarnType(TYPE_UNREAD_MESSAGE);
                        EventHelper.postUnread(JSON.toJSONString(model));
                        break;
                    case MessageId.roomFollowedLiveStartNotify:
                        JSONObject json = JSONObject.parseObject(object.socket_content);
                        EventHelper.postDynamicsNotify(json.getString("user"));
                        break;
                    case MessageId.userNewWarnNotify:
                        JSONObject jsonObject = JSONObject.parseObject(object.socket_content);
                        EventHelper.postUnread(jsonObject.getString("NewData"));
//                        Log.e("userNewWarnNotify", protoResult.content);
                        break;
                    case MessageId.userMoneyUpdateNotify:
                        EventHelper.postProfileMoneyInfo(object.socket_content);
//                        EventHelper.postRechargeSuccess();
//                        Log.e("userMoneyUpdateNotify", object.stringData + "");
                        break;
                    case MessageId.gameMailAddNotify:
                        //这里不管系统通知的数据, 只要有通知显示红点
                        notifySystem(object.stringData);
                        break;
                    case MessageId.userUpdataLvNotify:
                        AccountManager.getInstance().levelUpgrade(object.socket_content);
                        EventHelper.postProfileLevelInfo(object.socket_content);
                        break;
                    case MessageId.userCallLiveBubbleNotify:
                        JSONObject callLiveJson = JSONObject.parseObject(object.socket_content);
                        int count = callLiveJson.getInteger("Count");
                        UnreadManager.getInstance().postUnreadEvent(TYPE_UNREAD_CALL_LIVE_PROFILE, count);
                        break;
                    // 家族通知
                    case MessageId.familyUserApplyNotify:
                        FamilyManager.familyUserApply(object.stringData);
                        break;
                    case MessageId.familyLvUpgradeNotify:
                        FamilyManager.familyLvUpgrade(object.stringData);
                        break;
                    case MessageId.familyCreateNotify:
                        FamilyManager.familyCreate(object.stringData);
                        break;
                    case MessageId.familyEditNotify:
                        FamilyManager.familyEdit(object.stringData);
                        break;
                    case MessageId.familyUserManagementNotify:
                        FamilyUserManagementNotify managementNotify = JSON.parseObject(object.stringData, FamilyUserManagementNotify.class);
                        if (managementNotify.type == FamilyParams.FAMILYNOTIFY_CHANGE_ACTION) {
                            FamilyUserChangeManager.elderChange();
                        } else {
                            if (managementNotify.type == FamilyParams.FAMILYNOTIFY_KIOUT_ACTION) {
                                String account = FamilyAccountManager.getAccount();
                                if (!TextUtils.isEmpty(account)) {
                                    NIMClient.getService(MsgService.class).clearUnreadCount(account, SessionTypeEnum.Team);
                                }
                            }
                            FamilyUserChangeManager.userRefresh();
                        }
                        break;
                    case MessageId.familyLockedNotify:
                        FamilyManager.familyLocked(object.stringData);
                        break;
                }
            }
        });
        mSocketNotify.addEventListenWithTBASObject();
    }

    public void notifySystem(String stringData) {
        UnreadModel unreadModel = new UnreadModel();
        unreadModel.setWarnType(TYPE_UNREAD_SYSTEM);
        unreadModel.setValue(1);
        unreadModel.setData(stringData);
        EventHelper.postUnread(JSON.toJSONString(unreadModel));
    }

    public void postSystemRed() {
        UnreadModel unreadModel = new UnreadModel();
        unreadModel.setWarnType(TYPE_UNREAD_SYSTEM_PROFILE);
        unreadModel.setValue(1);
        EventHelper.postUnread(JSON.toJSONString(unreadModel));
    }

    public void unregisterSocketListener() {
        if (mSocketNotify != null) {
            mSocketNotify.destroy();
        }
    }

    public void unregisterLoginLoseListener() {
        if (mNoLoginNotify != null) {
            mNoLoginNotify.destroy();
        }
    }

////    public RecentContactInfo(String userChannelId, long yourUid, String yourNick, String yourImgHead, int yourUserLv, String latestMsg, int latestMsgTime, int unReadRecordCount) {
//    public static RecentContactInfo convertRecentContactInfo(NotifyMessageModel model){
//        RecentContactInfo recentContactInfo = new RecentContactInfo(model.getUserChannelId(),model.getUid(),model.getImgHead(),model.get)
//    }


}
